﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;
using DevExpress.Web.ASPxUploadControl;
using DevExpress.Web.ASPxClasses.Internal;

namespace Customer_Profile.eCard.Settle
{
    public partial class TransferAdd : System.Web.UI.Page
    {
        string strUserName = "";
        string strUserID = "";
        string strEntity = "";
        string settle_idList;

        const string UploadDirectory = "~/eCard/tempUpload/";
        const int ThumbnailSize = 100;

        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }

            if (Request["settle_id"] != null)
            {
                settle_idList = Request["settle_id"].ToString();
            }

            if (!Page.IsPostBack)
            {
                txtTitle.Text = DateTime.Now.ToString("yyyyMMdd")+"付款";
            }
            bindGridSettle(settle_idList);
        }

        private void bindGridSettle(string settle_idList)
        {
            try
            {
                DataSet ds = null;
                ds = DealDao.getSettleDealsList(settle_idList);
                gridSettle.DataSource = ds.Tables[0];
                gridSettle.DataBind();

                txtAmount.Text = ds.Tables[1].Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnTransfer_Click(object sender, EventArgs e)
        {
            if (txtAmount.Text.Trim().Equals(""))
            {
                lblWarning.Text = "转账金额不能为空";
                return;
            }
            try
            {
                string attach_url = uploadAttach();
                string status = "";
                string message = "";
                decimal dAmount = decimal.Parse(txtAmount.Text);
                DealDao.SettleTransfer(settle_idList, txtTitle.Text, strUserID, strUserName, dAmount, attach_url, txtRemark.Text, out status, out message);
                if (status.Equals("Y"))
                {
                    lblWarning.Text = message;
                    KVB.Common.JavaScript.ViewMsgGoUrl("SettleTransfer.aspx", "转账成功！");
                }
                else
                {
                    lblWarning.Text = message;
                }
            }
            catch (Exception ex)
            {
                lblWarning.Text = ex.ToString();
            }
        }

        private string uploadAttach()
        {
            
            string view_name = "";
            string attach_name = "";
            string attach_type = "";
            string attach_size = "";
            string attach_url = "";
            if (FileUpload1.HasFile)
            {
                FileInfo fileInfo = new FileInfo(FileUpload1.FileName);
                attach_name = DateTime.Now.ToString("yyyyMMddHHmmssfff") + fileInfo.Name;
                string savePathName = MapPath(UploadDirectory) + attach_name;
                FileUpload1.SaveAs(savePathName);

                view_name = fileInfo.Name;
                attach_type = FileUpload1.PostedFile.ContentType.ToString();
                attach_size = FileUpload1.PostedFile.ContentLength / 1024 + "K";
                attach_url = "/eCard/tempUpload/" + attach_name;
                //AttachDao.AddAttach(deal_no, view_name, attach_name, attach_type, attach_size, attach_url, "IA");
            }
            return attach_url;            
        }
    }
}